package com.xty.xtyle.frames;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.xty.xtyle.R;
import com.xty.xtyle.base.AbsFragment;
import com.xty.xtyle.base.FmPagerAdapter;
import com.xty.xtyle.base.MixTabFragment;
import com.xty.xtyle.base.TabFragment;
import com.xty.xtyle.db.ClothTypes;
import com.xty.xtyle.db.ClothTypesDao;
import com.xty.xtyle.db.cloth.Cloth;
import com.xty.xtyle.db.cloth.ClothDao;
import com.xty.xtyle.db.mix.MixCloth;
import com.xty.xtyle.db.mix.MixDao;
import com.xty.xtyle.db.mix.MixType;
import com.xty.xtyle.db.mix.MixTypeDao;
import com.xty.xtyle.detail.AddMixActivity;
import com.xty.xtyle.detail.ClothDetailActivity;
import com.xty.xtyle.event.FreshMixEvent;
import com.xty.xtyle.mix.MixDetialActivity;
import com.xty.xtyle.mix.MixPicActivity;
import com.xty.xtyle.utils.CommonUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class MixFragment extends AbsFragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FmPagerAdapter pagerAdapter;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private List<MixType> types;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_mix;
    }

    @Override
    public void initView() {
        initBar();
        getTypes();
        init();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void freshData() {

    }
    //右上角添加混搭动作
    void initBar() {
        findViewById(R.id.right_icon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), AddMixActivity.class));
            }
        });

        tabLayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.viewpager);
    }

    //获取混搭的场景：日常，假期等
    void getTypes() {
        MixTypeDao dao = new MixTypeDao();
        types = dao.findAll();
    }
    //初始化混搭数据，通过场景
    private void init() {

        //根据tab数量生成tablayout内容
        for(int i=0;i<types.size();i++){

            MixTabFragment tabFragment = new MixTabFragment();
            Bundle args = new Bundle();
            args.putString("type", types.get(i).getType());
            tabFragment.setArguments(args);
            fragments.add(tabFragment);
            tabLayout.addTab(tabLayout.newTab());
        }

        //将数据添加到界面，视图列表中
        tabLayout.setupWithViewPager(viewPager,false);
        pagerAdapter = new FmPagerAdapter(fragments, getChildFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        for(int i=0;i<types.size();i++){
            tabLayout.getTabAt(i).setText(types.get(i).getType());
        }
    }
}
